package com.lory.springcloud.controller;

import com.lory.springcloud.entities.CommonResult;
import com.lory.springcloud.entities.Payment;
import com.lory.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping("/create")
    public CommonResult<Integer> create(@RequestBody Payment payment){
        int result = paymentService.create(payment);
        log.info("****支付表插入结果：{}", result);
        if (result > 0){
            return new CommonResult<Integer>(200, "插入数据库成功,端口为 " + serverPort , result);
        }else {
            return new CommonResult<Integer>(200, "插入数据库失败,端口为 " + serverPort, null);
        }
    }

    @GetMapping("/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("****查询结果: {}", payment);
        return new CommonResult<Payment>(200, "查询成功,端口为 " + serverPort, payment);
    }

    @GetMapping("/payment/lib")
    public String getPaymentLB(){
        return serverPort;
    }

    @GetMapping("/feign/timeout")
    public String feignTimeout(){
        try {
            Thread.sleep(3000);
            return serverPort;
        } catch (InterruptedException e) {
            log.error(e.getMessage());
            return "error" + serverPort;
        }
    }

    @GetMapping("/zipkin")
    public String paymentZipkin(){
        return "hi, i am paymentzipkin server fall back,welcome to lory house";
    }
}

